//
//  $Id: NSDictionary+XMLAdditions.m 2 2007-09-07 18:35:43Z samson.etienne $
//  TCUBridgedClasses
//
//  Created by Simon Stapleton on Sat Nov 02 2002.
//  Copyright (c) 2002 Simon Stapleton. All rights reserved.
//

#import "NSDictionary+XMLAdditions.h"


@implementation NSDictionary (XMLAdditions)
/*"XML related addtions to NSDictionary"*/
/*"Given aString in XML attribute format (name="value" ...), generate a dictionary."*/
+ (id) dictionaryWithXMLAttributeString: (NSString *) aString {
  NSMutableDictionary * dict = [NSMutableDictionary dictionary];
  id quoteSet = [NSCharacterSet characterSetWithCharactersInString:@"\"\'"];
  id wsSet = [NSCharacterSet whitespaceCharacterSet];
  id scanner = [NSScanner scannerWithString:[aString stringByTrimmingCharactersInSet:wsSet]];

  while (![scanner isAtEnd]) {
    NSString * name;
    NSString * value;
    [scanner scanUpToString:@"=" intoString:&name];
    [scanner scanUpToCharactersFromSet:quoteSet intoString:nil];
    [scanner scanCharactersFromSet:quoteSet intoString:nil];
    [scanner scanUpToCharactersFromSet:quoteSet intoString:&value];
    [scanner scanCharactersFromSet:quoteSet intoString:nil];

    if (name && value) {
      [dict setObject:[value stringByTrimmingCharactersInSet:wsSet] forKey:[name stringByTrimmingCharactersInSet:wsSet]];
    }
  }
  return [[dict copy] autorelease];
      
}
@end
